# game_loop.py

import game_defs

def game_loop():
    game_defs.print_board()

    while True:
        try:
            command = input('请输入您的指令（例如：落子：p3D，移动棋子：m3D4E，吃子：c4E）：')


            # 将输入的坐标转换为大写字母
            command = command.upper()

            action = command[0]
            if action == 'P':
                if len(command) != 3:
                    raise ValueError('无效的落子指令')
                row = int(command[1]) - 1
                col = ord(command[2].upper()) - ord('A')
                if not game_defs.place_piece(row, col, 'X'):
                    raise ValueError('落子失败')
            elif action == 'M':
                if len(command) != 5:
                    raise ValueError('无效的移动棋子指令')
                src_row = int(command[1]) - 1
                src_col = ord(command[2].upper()) - ord('A')
                dst_row = int(command[3]) - 1
                dst_col = ord(command[4].upper()) - ord('A')
                if not game_defs.move_piece(src_row, src_col, dst_row, dst_col):
                    raise ValueError('移动棋子失败')
            elif action == 'C':
                if len(command) != 3:
                    raise ValueError('无效的吃子指令')
                row = int(command[1]) - 1
                col = ord(command[2].upper()) - ord('A')
                if not game_defs.capture_piece(row, col):
                    print('吃子失败')
                    continue
            else:
                print('无效的指令')
                continue

            game_defs.print_board()
            # TODO: 判断游戏是否结束，输出胜负结果并结束循环

        except ValueError as e:
            print(str(e))
            continue
